#!/bin/bash

if [ ! -f "ttyd" ]; then
    ttyd_name="ttyd_linux.$( uname -m )"
    if [ ! -f "${ttyd_name}" ]; then
        curl -L https://github.com/tsl0922/ttyd/releases/download/1.6.1/"${ttyd_name}" -o "${ttyd_name}"
    fi
    cp -r ./"${ttyd_name}" ./ttyd && chmod -R 755 ./ttyd
fi

if [ ! -n "$1" ]; then
    ./ttyd -p 8686 login bash
fi

# first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then
    set -- ./ttyd "$@"
fi

exec "$@"
